<template>
  <el-card class="box-card">
    <template #header>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="入库单号">
          <el-input v-model="formInline.inStoreSn" placeholder="请输入入库单号" clearable />
        </el-form-item>
        <el-form-item label="入库计划单号">
          <el-input v-model="formInline.planSn" placeholder="请输入入库计划单号" clearable />
        </el-form-item>
        <el-form-item label="审核状态">
          <el-select v-model="formInline.region" placeholder="请选择审核状态" clearable>
            <el-option label="待审核" :value="0" />
            <el-option label="审核不通过" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="供应商">
          <selectSupplier v-model="formInline.supplierId"></selectSupplier>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
          <el-button @click="init">重置</el-button>
        </el-form-item>
      </el-form>
    </template>

    <el-table :data="tableParams.rows">
      <el-table-column prop="batchNumber" label="批次号" />
      <el-table-column prop="cargoSn" label="货物编码" />
      <el-table-column prop="cargoName" label="货物名称" />
      <el-table-column prop="unitPrice" label="单价" />
      <el-table-column prop="num" label="数量" />
      <el-table-column prop="address" label="金额">
        <template #default="scope">
          {{(scope.row.unitPrice*scope.row.num )||0}}
        </template>
      </el-table-column>
      <el-table-column prop="address" label="单位">
        <template #default="scope">
          <selectUnit v-model="scope.row.cargoUnit" readonly="1" disabled></selectUnit>
        </template>
      </el-table-column>
      <el-table-column prop="trayName" label="托盘码" />
      <el-table-column prop="factoryName" label="工厂" />
      <el-table-column prop="storeName" label="仓库" />
      <el-table-column prop="storeRegionName" label="库区" />
      <el-table-column prop="storeRegionLocationName" label="库位" />
      <el-table-column prop="cargoSpec" label="规格" />
      <el-table-column prop="address" label="货物属性">
        <template #default="scope">
          {{scope.row.type==1?"良品":"不良品"}}
        </template>
      </el-table-column>
      <el-table-column prop="inStoreSn" label="入库单" />
      <el-table-column prop="supplierName" label="供应商" />
      <el-table-column align="center" label="操作" fixed="right">
        <template #default="scope">
          <el-button type="primary" text @click="handleClick(scope.row)">核验</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-if="tableParams['total']" :total="tableParams['total']" v-model:page="form.pageNum" v-model:limit="form.pageSize" @pagination="getInfo" />

    <el-dialog v-model="dialogVisible" title="审核" width="50%" :before-close="handleClose">
      <el-form :model="form">
        <el-form-item label="审核状态">
          <el-radio-group v-model="form.processStatus">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见">
          <el-input v-model="form.processRemark" :autosize="{ minRows:  4 }" type="textarea" placeholder="请输入审核意见" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="save">
            确 认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup lang="ts">
import { onMounted } from "vue";

const { proxy } = getCurrentInstance();
let loading = ref(false);
let formInline = ref({
  pageNum: 1,
  pageSize: 10,
  inStoreSn: "",
  planSn: "",
  supplierId: "",
  processStatus: null,
});
let tableParams = ref({});
let form = ref({
  id: "",
  processStatus: 1,
  processRemark: "",
});
let dialogVisible = ref(false);

onMounted(() => {
  search();
});

let getInfo = async () => {
  tableParams.value = await proxy.$apis.inWarehousDetailProcessList(
    formInline.value
  );
};
let search = () => {
  formInline.value.pageNum = 1;
  getInfo();
};
let init = async () => {
  formInline.value = {
    pageNum: 1,
    pageSize: 10,
    inStoreSn: "",
    planSn: "",
    supplierId: "",
    processStatus: null,
  };
  getInfo();
};
let handleClick = (row) => {
  form.value.id = row.id;
  dialogVisible.value = true;
};
let save = async () => {
  try {
    loading.value = true;
    await proxy.$apis.inWarehousDetailProcessAction(form.value);
    proxy.$utils.showToast("操作成功");
    handleClose();
  } catch (error) {
  } finally {
    loading.value = false;
  }
};
let handleClose = () => {
  form.value = {
    id: "",
    processStatus: 1,
    processRemark: "",
  };
  init();
  dialogVisible.value = false;
};
</script>

<style lang="scss" scoped>
.box-card {
  margin: 20px;
  .el-form {
    .el-form-item {
      margin-bottom: 5px !important;
    }
  }
}
</style>
